// 宠物状态
const petState = {
    x: 100,
    y: 100,
    happiness: 100,
    hunger: 100,
    isSleeping: false,
    isDragging: false,
    dragOffset: { x: 0, y: 0 }
};

// DOM元素
const petContainer = document.getElementById('pet-container');
const pet = document.getElementById('pet');
const speechBubble = document.getElementById('speech-bubble');
const contextMenu = document.getElementById('context-menu');
const happinessElement = document.getElementById('happiness');
const hungerElement = document.getElementById('hunger');

// 初始化位置
petContainer.style.left = petState.x + 'px';
petContainer.style.top = petState.y + 'px';

// 拖拽功能
petContainer.addEventListener('mousedown', startDrag);
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', stopDrag);

function startDrag(e) {
    if (e.button !== 0 || e.target.id === 'context-menu') return; // 排除右键菜单

    petState.isDragging = true;
    const rect = petContainer.getBoundingClientRect();
    petState.dragOffset.x = e.clientX - rect.left;
    petState.dragOffset.y = e.clientY - rect.top;
    petContainer.style.cursor = 'grabbing';
    e.preventDefault();
}

function drag(e) {
    if (!petState.isDragging) return;

    petState.x = e.clientX - petState.dragOffset.x;
    petState.y = e.clientY - petState.dragOffset.y;

    // 限制在窗口范围内
    const maxX = window.innerWidth - petContainer.offsetWidth;
    const maxY = window.innerHeight - petContainer.offsetHeight;

    petState.x = Math.max(0, Math.min(petState.x, maxX));
    petState.y = Math.max(0, Math.min(petState.y, maxY));

    petContainer.style.left = petState.x + 'px';
    petContainer.style.top = petState.y + 'px';
}

function stopDrag() {
    petState.isDragging = false;
    petContainer.style.cursor = 'move';
}

// 右键菜单
petContainer.addEventListener('contextmenu', function(e) {
    e.preventDefault();
    contextMenu.style.display = 'block';
    contextMenu.style.left = (e.clientX - petContainer.getBoundingClientRect().left) + 'px';
    contextMenu.style.top = (e.clientY - petContainer.getBoundingClientRect().top) + 'px';
});

document.addEventListener('click', function() {
    contextMenu.style.display = 'none';
});

// 宠物交互功能
document.getElementById('feed-btn').addEventListener('click', function() {
    petState.hunger = Math.min(100, petState.hunger + 20);
    updateStatus();
    showMessage('好吃！谢谢！');
});

document.getElementById('play-btn').addEventListener('click', function() {
    petState.happiness = Math.min(100, petState.happiness + 15);
    petState.hunger = Math.max(0, petState.hunger - 5);
    updateStatus();
    showMessage('真好玩！');
    animatePlay();
});

document.getElementById('sleep-btn').addEventListener('click', function() {
    petState.isSleeping = !petState.isSleeping;
    if (petState.isSleeping) {
        showMessage('Zzz...');
        document.querySelector('.pet-body').style.background = '#ddd';
    } else {
        showMessage('我醒啦！');
        document.querySelector('.pet-body').style.background = '#ffcc00';
    }
});

document.getElementById('close-btn').addEventListener('click', function() {
    if (confirm('确定要关闭宠物吗？')) {
        window.close(); // 在Electron中会关闭应用
    }
});

// 点击宠物互动
pet.addEventListener('click', function(e) {
    if (e.target === pet || e.target.classList.contains('pet-body')) {
        petState.happiness = Math.min(100, petState.happiness + 5);
        updateStatus();
        showRandomMessage();
        animatePet();
    }
});

// 显示消息
function showMessage(message) {
    speechBubble.textContent = message;
    speechBubble.style.opacity = '1';
    setTimeout(() => {
        speechBubble.style.opacity = '0';
    }, 2000);
}

function showRandomMessage() {
    const messages = [
        '你好！',
        '今天过得怎么样？',
        '摸摸我！',
        '我饿了...',
        '陪我玩吧！',
        '好无聊啊',
        '喜欢你！'
    ];
    const randomMessage = messages[Math.floor(Math.random() * messages.length)];
    showMessage(randomMessage);
}

// 更新状态显示
function updateStatus() {
    happinessElement.textContent = petState.happiness;
    hungerElement.textContent = petState.hunger;
}

// 宠物动画
function animatePet() {
    pet.style.transform = 'scale(1.1)';
    setTimeout(() => {
        pet.style.transform = 'scale(1)';
    }, 200);
}

function animatePlay() {
    const limbs = document.querySelectorAll('.pet-limb');
    limbs.forEach(limb => {
        limb.style.animation = 'jump 0.3s ease';
    });
    setTimeout(() => {
        limbs.forEach(limb => {
            limb.style.animation = '';
        });
    }, 300);
}

// 眼球跟随鼠标
document.addEventListener('mousemove', function(e) {
    if (petState.isSleeping) return;

    const pupils = document.querySelectorAll('.pupil');
    const petRect = pet.getBoundingClientRect();
    const petCenterX = petRect.left + petRect.width / 2;
    const petCenterY = petRect.top + petRect.height / 2;

    pupils.forEach(pupil => {
        const eyeRect = pupil.parentElement.getBoundingClientRect();
        const eyeCenterX = eyeRect.left + eyeRect.width / 2;
        const eyeCenterY = eyeRect.top + eyeRect.height / 2;

        const deltaX = e.clientX - eyeCenterX;
        const deltaY = e.clientY - eyeCenterY;
        const angle = Math.atan2(deltaY, deltaX);

        const distance = Math.min(5, Math.sqrt(deltaX * deltaX + deltaY * deltaY) / 20);

        const pupilX = Math.cos(angle) * distance;
        const pupilY = Math.sin(angle) * distance;

        pupil.style.transform = `translate(${pupilX}px, ${pupilY}px)`;
    });
});

// 状态随时间变化
setInterval(() => {
    if (!petState.isSleeping) {
        petState.happiness = Math.max(0, petState.happiness - 0.5);
        petState.hunger = Math.max(0, petState.hunger - 0.3);
        updateStatus();

        if (petState.happiness < 30) {
            showMessage('我不开心...');
        }

        if (petState.hunger < 30) {
            showMessage('我饿了...');
        }
    }
}, 5000);

// 设置按钮事件处理
const settingsBtn = document.getElementById('settings-btn');
settingsBtn.addEventListener('click', () => {
    const { ipcRenderer } = require('electron');
    ipcRenderer.invoke('open-settings');
});
